package com.sjy.MyBlog.biz.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sjy.MyBlog.bean.Result;
import com.sjy.MyBlog.bean.Subscribe;
import com.sjy.MyBlog.bean.SubscribeExample;
import com.sjy.MyBlog.biz.SubscribeBiz;
import com.sjy.MyBlog.dao.SubscribeMapper;

@Service
public class SubscribeBizImpl implements SubscribeBiz {

	@Resource
	private SubscribeMapper sm;
	
	@Override
	public Result addSubscribe(Subscribe subscribe) {
		List<Subscribe> subs = selectSubscribe();
		for (Subscribe sub : subs) {
			if(sub.getEmail().equals(subscribe.getEmail())) {
				return new Result(0, "您已订阅");
			}
		}
		int resultNum = sm.insert(subscribe);
		Result result;
		if(resultNum>0) {
			result = new Result(1, "感谢订阅");
		}else {
			result = new Result(0, "订阅失败");
		}
		return result;
	}

	@Override
	public List<Subscribe> selectSubscribe() {
		return sm.selectByExample(null);
	}

	@Override
	public Result cancleSubscribe(Subscribe subscribe) {
		SubscribeExample example = new SubscribeExample();
		example.createCriteria().andEmailEqualTo(subscribe.getEmail());
		int resultNum = sm.deleteByExample(example);

		Result result;
		if(resultNum>0) {
			result = new Result(1, "感谢陪伴");
		}else {
			result = new Result(0, "取消失败");
		}
		return result;
	}

}
